<template>
  <div>
    <el-page-header @back="goBack" content="需求列表">
    </el-page-header>

    <el-divider></el-divider>

    <el-row>
      <el-col>
        <el-button type="primary" @click="addReq">提出需求</el-button>
      </el-col>
    </el-row>

    <el-divider><i class="el-icon-tickets"></i></el-divider>

    <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
    >
      <el-table-column label="编号" width="80" align="center">
        <template scope="scope">
          <span>{{ scope.$index + 1 }} </span>
        </template>
      </el-table-column>
      <el-table-column prop="reqName" label="需求名称" align="center"/>
      <el-table-column prop="nickname" label="发表者" width="150" align="center"/>
      <el-table-column prop="proName" label="项目名称" align="center"/>
      <el-table-column prop="distributed" label="负责人" width="150" align="center"/>
      <el-table-column prop="createDate" label="提交时间" width="180" align="center"/>
      <el-table-column label="详情" width="80" align="center">
        <template slot-scope="scope">
          <el-button type="primary"
                     icon="el-icon-tickets"
                     circle
                     size="mini"
                     @click="knowDetails(scope.row.id)"
          />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button
              type="primary"
              icon="el-icon-edit"
              circle
              size="mini"
              @click="handleEdit(scope.row)"
          />
          <el-button
              type="danger"
              icon="el-icon-delete"
              circle
              size="mini"
              @click="openDeleteConfirm(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {
      proId: '',
      list: [],
      listLoading: true
    }
  },
  methods: {
    addReq() {
      this.$router.push({path: '/req/add-new'});
    },
    goBack() {
      this.$router.push({path: '/projects/getProjectList'});
    },
    /* 详情 */
    knowDetails(id) {
      this.$router.push({path: '/req/details', query: {id}})
    },
    /* 修改 */
    handleEdit(req) {
      let url = 'http://localhost:8989/reqs/' + req.userId + '/term';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        } else {
          let id = req.id;
          this.$router.push({path: '/req/update', query: {id}})
        }
      })
    },
    /* 删除 */
    handleDelete(req) {
      let url = 'http://localhost:8989/reqs/' + req.id + '/delete';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        console.log(responseBody);
        if (responseBody.state != 20000) {
          this.$message.error(responseBody.message);
        }
        if (responseBody.state == 20000 || responseBody.state == 40400) {
          this.loadReqList(this.proId);
        }
      })
    },
    openDeleteConfirm(req) {
      const message = '此操作将删除【' + req.knowledge + '】知识点，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = 'http://localhost:8989/reqs/' + req.userId + '/term';
        this.axios
            .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
            .post(url).then((response) => {
          let responseBody = response.data;
          if (responseBody.state != 20000) {
            this.$message.error(responseBody.message);
          } else {
            this.handleDelete(req);
          }
        })
      }).catch(() => {
      });
    },
    /* 首次访问查询知识点列表 */
    loadReqList(proId) {
      console.log('loadKnowList:' + proId);
      // let url = 'http://localhost:8989/reqs/' + proId + '/list';
      let url = 'http://localhost:8989/reqs/1586243542765494273/list';
      console.log('url = ' + url);
      this.listLoading = true;
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.list = responseBody.data;
        console.log(this.list);
        this.listLoading = false;
      })
    }
  },
  mounted() {
    this.proId = this.$route.query.id;
    console.log('mounted');
    this.loadReqList(this.proId);
  }
}

</script>
